'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, STRING, TEXT, DATE } = Sequelize;
    
    await queryInterface.createTable('ui_configs', {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      user_id: {
        type: INTEGER,
        allowNull: false,
        comment: '用户ID'
      },
      page_key: {
        type: STRING(100),
        allowNull: false,
        comment: '页面标识符'
      },
      config_type: {
        type: STRING(50),
        allowNull: false,
        comment: '配置类型: filter(筛选条件), column(列设置), pagination(分页信息)'
      },
      config_key: {
        type: STRING(100),
        allowNull: false,
        comment: '配置键名'
      },
      config_value: {
        type: TEXT,
        allowNull: true,
        comment: '配置值'
      },
      created_at: {
        type: DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      },
      updated_at: {
        type: DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
      }
    });
    
    // 添加索引
    await queryInterface.addIndex('ui_configs', ['user_id', 'page_key', 'config_type'], {
      name: 'idx_ui_configs_user_page_type'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('ui_configs');
  }
};